<template>
  <!-- 插槽基础知识总结：
      1.默认插槽 =》1个 默认名字叫default  =》传入结构
      2.具名插槽 =》多个 自己起的名字 =》传入结构
      3.作用域插槽 =》基于前两种=》接收数据（子传父）
  -->
  <div class="deparentment-container">
    <div class="app-container">
      <!----------------------------- 头部区域, 具名插槽 ----------------------------->
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <template #header>
          <!-- 用一个行列布局 -->
          <el-row>
            <el-col :span="20">
              <span>{{ company.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>负责人</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="addDept()">
                        添加子部门
                      </el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
        <!---------------------------- 下部主体内容区 ------------------------------->
        <!-- default-expand-all=true 展开所有部门 accordion: 每次只打开一个同级树节点展开 -->
        <el-tree :data="treeData" :props="defaultProps" accordion>
          <!-- 作用域插槽 data 拿到的是每一个子节点的对象 -->
          <template #default="{ data }">
            <el-row style="width: 100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <!-- 操作按钮 -->
                        <el-dropdown-item @click.native="addDept(data, 1)">
                          添加子部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="addDept(data, 2)">
                          编辑部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="delData(data)">
                          删除部门
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!---------------------- 增加部门组件 --------------------->
    <addDept
      ref="editDept"
      :curr-dept="currDept"
      :show="show"
      :default-data="defaultData"
      @updata-dept="getDepartments"
      @close-popup="show = false"
    ></addDept>
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { transformTreeData } from '@/utils'
// 导入增加部门组件
import addDept from './components/add-dept.vue'
export default {
  components: {
    addDept
  },
  data () {
    return {
      defaultData: [],
      show: false, // 定义弹层显示隐藏
      treeData: [], // 组织架构, 树形数据
      currDept: null, // 当前操作部门(父)
      company: { name: '北京凌云科技有限责任公司', manager: '' },
      // 修改树形中属性名=》场景：后台返回数据属性名不叫lable，子不叫children
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created () {
    this.getDepartments()
  },
  methods: {
    // --------------------- 获取部门组织架构数据 ----------------------[查]
    async getDepartments () {
      const { depts } = await getDepartments()
      this.treeData = transformTreeData(depts)
      this.defaultData = depts
    },

    // ------------------------- 删除部门数据 ---------------------------[删]
    async delData (data) {
      /**
       * data: 当前子部门
       * 1.提示
       * 2.如果有children 说明是一级部门, 且存在子部门 => 提示不能直接删除
       * 3.否则, 调接口删除
       * 4.重新请求数据, 渲染页面
       */
      try {
        await this.$confirm(`确认要删除${data.name}吗？`, '提示')
        if (data.children && data.children.length > 0) {
          this.$message.error('该部门包含下属部门不能删，请先删除下属部门！')
        }
        // 调接口, 删除
        await delDepartments(data.id)
        // 重新请求数据, 渲染页面
        this.getDepartments()
      } catch (error) {
        console.log(error)
      }
    },

    // 添加子级部门
    addDept (currData, type) {
      this.show = true
      this.currDept = currData
      if (type === 2) {
        // console.log('编辑')
        this.$refs.editDept.getDepartDetail(this.currDept.id)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-tree-node__content {
  margin: 3px 0;
  font-size: 14px;
}
</style>
